<template>
<div>
 <v-nav :path="path" :title="title"></v-nav>  

<div class="content">
  <div class="list-block">
    <ul>
      <!-- Text inputs -->
      <li>
        <div class="item-content">
          <div class="item-media"><i class="icon icon-form-name"></i></div>
          <div class="item-inner">
            <div class="item-title label">俱乐部名称</div>
            <div class="item-input">
              <input type="text" v-model="club_name" placeholder="俱乐部名称" readOnly="true">
            </div>
          </div>
        </div>
      </li>

       <li>
        <div class="item-content">
          <div class="item-media"><i class="icon icon-form-email"></i></div>
          <div class="item-inner">
            <div class="item-title label">我的房卡数量</div>
            <div class="item-input">
              <input type="number"  placeholder="我的房卡数" v-model="user_gems" readOnly="true">
            </div>
          </div>
        </div>
      </li>
      <li>
        <div class="item-content">
          <div class="item-media"><i class="icon icon-form-email"></i></div>
          <div class="item-inner">
            <div class="item-title label">俱乐部房卡数量</div>
            <div class="item-input">
              <input type="number"  v-model="club_gems" placeholder="房卡数" value='100' readOnly="true">
            </div>
          </div>
        </div>
      </li> 

       <li>
        <div class="item-content">
          <div class="item-media"><i class="icon icon-form-gender"></i></div>
          <div class="item-inner">
            <div class="item-title label">操作类型</div>
            <div class="item-input">
              <select id="charteType"> 
                <option value="1" selected = "selected" >充值</option>
                <option value="2">扣减</option>
              </select>
            </div>
          </div>
        </div>
      </li>
      <li>
        <div class="item-content">
          <div class="item-media"><i class="icon icon-form-password"></i></div>
          <div class="item-inner">
            <div class="item-title label">操作房卡数</div>
            <div class="item-input">
              <input type="text" id="gems"  class="" >
            </div>
          </div>
        </div>
      </li>
     
      
    </ul>
  </div>
  <div class="content-block">
    <div class="row">
      <div class="col-50"><a href="#" class="button button-big button-fill button-danger">取消</a></div>
      <div class="col-50"><a href="#" class="button button-big button-fill button-success" @click="updateClub()">提交</a></div>
    </div>
  </div>
</div>

  
</div>
</template>

<script>
import $ from 'zepto'
import VNav from '../../components/Nav'
import _global from '../global'

export default {
  route: {
    data ({from, next}) {
      if (from.path === '/user') {
        
        this.path = from.path
      }
    }
  },
  ready () {
    
    if (window.localStorage.getItem('user_id')) {
      self.userId = parseInt(window.localStorage.getItem('user_id'), 0)
    }
    $.init()

    this.getUserGems()
    this.club_name = this.$route.params.club_name
    this.club_gems = this.$route.params.club_gems
  
  },
  data () {
    return {
      path: '/user/club',
      title: '俱乐部房卡管理',
      predisabled: '',
      active: 'active',
      nextdisabled: '',
      submit:false,
      user_gems:100,
      club_name:'dfadsfasdf',
      club_gems:100

    }
  },
  methods: {

    //创建俱乐部
   updateClub(){
    let self = this

     if(self.submit){
        $.alert('已经提交了');
        return
      }
    
      let gems =  $('#gems').val();
      let charge_type = $('#charteType').val();
      
      if(gems===0 || gems==''){
         $.alert('操作房卡数不能为0');
        return
      }

     gems = parseInt(gems);
    
      if(charge_type ==1 && gems > self.user_gems){
        $.alert('充值房卡不能大于用户房卡')
        return

      }

      if(charge_type ==2 && gems > self.club_gems){
        $.alert('扣减房卡不能大于俱乐部房卡')
        return

      }

      if (window.localStorage.getItem('user_id')) {
        self.userId = parseInt(window.localStorage.getItem('user_id'), 0)
        self.id = parseInt(window.localStorage.getItem('id'), 0)
      
      }
    
       self.submit=true

      let club_id = this.$route.params.club_id  
    
       this.$http.get(_global.url + '/updateClub?id=' + self.id +'&user_id='+self.userId +'&club_id='+club_id+'&charge_type='+charge_type+'&gems='+gems).then(function(res) {
        if (typeof res.data === 'string') {
          res.data = JSON.parse(res.data)
        }
         self.submit=false
        if (res.data.errcode === -999) {
          // this.$router.go('/')
          window.location.href = _global.url + '/wxlogin'
          return
        }

        if (res.data.errcode === 2) {
          // this.$router.go('/')
          $.alert('服务器忙，请稍后再试');
          return
        }

        if (res.data.errcode === 1) {
          // this.$router.go('/')
          $.alert('参数错误');
          return
        }

        if(res.data.errcode === 0){

          if(res.data.data.code === -1){
             $.alert('用户房卡不够 或者设置俱乐部房卡大于用户房卡');
             return
          }else if (res.data.data.code === -4){
             $.alert('服务器忙，请稍后再试');
             return
          }else{

            if(res.data.data.code==-1){
               $.alert('充值用户房卡不够');
               return
            }else if(res.data.data.code==-2){
               $.alert('扣减俱乐部房卡不够');
               return
            }else if(res.data.data.code==-4){
               $.alert('服务器忙，请稍后再试');
               return
            }else{
                $.toast('操作成功，正在跳转...');
                $('#club_name').val('');
                $('#gems').val(100)
                self.submit=false
               this.$router.go('/user/club')
               location.reload()
                return
            }
          }
         
        }
       
      }, function(res) {
        console.log(res)
      })

    },
    //获取用户房卡数
    
    getUserGems(){

      let self = this
        if (window.localStorage.getItem('user_id')) {
          self.userId = parseInt(window.localStorage.getItem('user_id'), 0)
          self.id = parseInt(window.localStorage.getItem('id'), 0)
         
      }

       this.$http.get(_global.url + '/getUserGems?id=' + self.id +'&user_id='+self.userId).then(function(res) {
        if (typeof res.data === 'string') {
          res.data = JSON.parse(res.data)
        }
        if (res.data.errcode === -999) {
         
          window.location.href = _global.url + '/wxlogin'
          return
        }

        if (res.data.errcode === 2) {
          
          $.alert('服务器忙，请稍后再试');
          return
        }

        if(res.data.errcode === 0){
          self.user_gems = res.data.data.gems;
          
          return
        }
       
      }, function(res) {
        console.log(res)
      })
    }
  },
  components: {
    VNav
  }
}
</script>

<style>

.label {
color:#333;
}
</style>
